$(function(){

	var phone_param = "phone";
	var get_coupon_status_url = "http://219.233.17.235:8008/api/coupon/group/list";
	var get_user_id_url = "http://219.233.17.235:8008/api/v1/common/findUserId";
	var get_coupon_url = "http://219.233.17.235:8008/api/coupon/bind";
	var success_url = "http://219.233.17.235:8008/event/list.html";
	var error_url = "http://219.233.17.235:8008/event/error.html";
	var over_url = "http://219.233.17.235:8008/event/over.html";

	var coupon_id = ["8a80848258c85fff0158dd18abd70003"];



	function getCouponsCallback(data){
		if(data.status == "SUCCESS"){
			if($.isEmptyObject(data.content.content) || data.content.content.length == 0){
				alert("查询不到优惠券信息！");
				location.href = error_url;
			}else{
				var hasCoupon = false;
				for(var i = 0; i < data.content.content.length; i++){
					var conArr = data.content.content[i];
					if(conArr.preCount > conArr.curCount){
						hasCoupon = true;
					}
				}
				if(!hasCoupon){
					alert("优惠券已发完！");
					location.href = over_url;
				}
			}
		}else{
			alert("获取优惠券信息失败！");
			location.href = error_url;
		}
	}


	// 基于优惠券id获取对应的优惠券发放数量
	$.ajax({
		type: "post",
		url: get_coupon_status_url,
		async : false,
		data: {
			params: JSON.stringify({
				ids: coupon_id,
				startTime: new Date().getTime()+"",
				page: 0,
				size: 10000
			})
		},
		success: getCouponsCallback
	});




	function getQueryString(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
		var r = window.location.search.substr(1).match(reg);
		if (r != null) return unescape(r[2]); return null;
	}

	// 把url中的参数赋值到input中
	var phoneNum = getQueryString(phone_param);

	if(phoneNum != null){
		$(".phone").val(phoneNum);
	}


	var user_id = "";
	function getUserIdCallback(data){
		if(data.id){
			user_id = data.id;
		}else{
			alert("获取用户ID失败！");
		}
	}

	function bindCouponCallback(data){
		if(data.status == "SUCCESS"){

			var isSuccess = false;
			var errMessage = [];

			for(var i = 0; i < data.content.length; i++){
				var contentArr = data.content[i];
				if(contentArr.result == "SUCCESS"){
					isSuccess = true;
				}else{
					errMessage.push(contentArr.message);
				}
			}

			if(isSuccess){
				alert("获取优惠券成功！");
				location.href = success_url + "?id=" + user_id;
			}else{
				alert(errMessage[0]);
			}
		}else if(data.status == "FAIL"){
			alert(data.message);
		}else{
			alert("获取优惠券异常！");
		}
	}

	$(".btn").click(function(){
		var phoneVal = $(".phone").val();

		if(phoneVal == ""){
			alert("请输入手机号");
		}

		if(!/^0?(13|14|15|18)[0-9]{9}$/.test(phoneVal)){
			alert("请输入正确的手机号");
		}

		// 获取用户id
		$.ajax({
			type: "get",
			async : false,
			url: get_user_id_url,
			data: {
				mobile: phoneVal
			},
			dataType: 'json',
			success: getUserIdCallback
		});

		// 判断用户id是否获取成功
		if(user_id == null || user_id == ""){
			alert("获取用户ID失败！");
		}else{
			// 绑定优惠券
			$.ajax({
				type: "post",
				url: get_coupon_url,
				data: {
					params: JSON.stringify({
						ids: coupon_id,
						userId: user_id
					})
				},
				success: bindCouponCallback
			});
		}

	});


});